home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / BitTorrent / btformats.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  3.5 KB  |  139 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. from types import StringType, LongType, IntType, ListType, DictType
  5. from re import compile
  6. reg = compile('^[^/\\\\.~][^/\\\\]*$')
  7. ints = (LongType, IntType)
  8.  
  9. def check_info(info):
  10.     if type(info) != DictType:
  11.         raise ValueError, 'bad metainfo - not a dictionary'
  12.     
  13.     pieces = info.get('pieces')
  14.     if type(pieces) != StringType or len(pieces) % 20 != 0:
  15.         raise ValueError, 'bad metainfo - bad pieces key'
  16.     
  17.     piecelength = info.get('piece length')
  18.     if type(piecelength) not in ints or piecelength <= 0:
  19.         raise ValueError, 'bad metainfo - illegal piece length'
  20.     
  21.     name = info.get('name')
  22.     if type(name) != StringType:
  23.         raise ValueError, 'bad metainfo - bad name'
  24.     
  25.     if not reg.match(name):
  26.         raise ValueError, 'name %s disallowed for security reasons' % name
  27.     
  28.     if info.has_key('files') == info.has_key('length'):
  29.         raise ValueError, 'single/multiple file mix'
  30.     
  31.     if info.has_key('length'):
  32.         length = info.get('length')
  33.         if type(length) not in ints or length < 0:
  34.             raise ValueError, 'bad metainfo - bad length'
  35.         
  36.     else:
  37.         files = info.get('files')
  38.         if type(files) != ListType:
  39.             raise ValueError
  40.         
  41.         for f in files:
  42.             if type(f) != DictType:
  43.                 raise ValueError, 'bad metainfo - bad file value'
  44.             
  45.             length = f.get('length')
  46.             if type(length) not in ints or length < 0:
  47.                 raise ValueError, 'bad metainfo - bad length'
  48.             
  49.             path = f.get('path')
  50.             if type(path) != ListType or path == []:
  51.                 raise ValueError, 'bad metainfo - bad path'
  52.             
  53.             for p in path:
  54.                 if type(p) != StringType:
  55.                     raise ValueError, 'bad metainfo - bad path dir'
  56.                 
  57.                 if not reg.match(p):
  58.                     raise ValueError, 'path %s disallowed for security reasons' % p
  59.                     continue
  60.             
  61.         
  62.         for i in xrange(len(files)):
  63.             for j in xrange(i):
  64.                 if files[i]['path'] == files[j]['path']:
  65.                     raise ValueError, 'bad metainfo - duplicate path'
  66.                     continue
  67.             
  68.         
  69.  
  70.  
  71. def check_message(message):
  72.     if type(message) != DictType:
  73.         raise ValueError
  74.     
  75.     check_info(message.get('info'))
  76.     announce = message.get('announce')
  77.     if type(announce) != StringType or len(announce) == 0:
  78.         raise ValueError, 'bad torrent file - announce is invalid'
  79.     
  80.  
  81.  
  82. def check_peers(message):
  83.     if type(message) != DictType:
  84.         raise ValueError
  85.     
  86.     if message.has_key('failure reason'):
  87.         if type(message['failure reason']) != StringType:
  88.             raise ValueError
  89.         
  90.         return None
  91.     
  92.     peers = message.get('peers')
  93.     if type(peers) == ListType:
  94.         for p in peers:
  95.             if type(p) != DictType:
  96.                 raise ValueError
  97.             
  98.             if type(p.get('ip')) != StringType:
  99.                 raise ValueError
  100.             
  101.             port = p.get('port')
  102.             if type(port) not in ints or p <= 0:
  103.                 raise ValueError
  104.             
  105.             if p.has_key('peer id'):
  106.                 id = p.get('peer id')
  107.                 if type(id) != StringType or len(id) != 20:
  108.                     raise ValueError
  109.                 
  110.             len(id) != 20
  111.         
  112.     elif type(peers) != StringType or len(peers) % 6 != 0:
  113.         raise ValueError
  114.     
  115.     interval = message.get('interval', 1)
  116.     if type(interval) not in ints or interval <= 0:
  117.         raise ValueError
  118.     
  119.     minint = message.get('min interval', 1)
  120.     if type(minint) not in ints or minint <= 0:
  121.         raise ValueError
  122.     
  123.     if type(message.get('tracker id', '')) != StringType:
  124.         raise ValueError
  125.     
  126.     npeers = message.get('num peers', 0)
  127.     if type(npeers) not in ints or npeers < 0:
  128.         raise ValueError
  129.     
  130.     dpeers = message.get('done peers', 0)
  131.     if type(dpeers) not in ints or dpeers < 0:
  132.         raise ValueError
  133.     
  134.     last = message.get('last', 0)
  135.     if type(last) not in ints or last < 0:
  136.         raise ValueError
  137.     
  138.  
  139.